//
//  OneLinePickerView.m
//  TooToo
//
//  Created by liuning on 16/1/18.
//  Copyright © 2016年 MoHao. All rights reserved.
//

#import "OneLinePickerView.h"
#import "FbeeAPP-Swift.h"

@interface OneLinePickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    NSArray * dataArray;
    UIPickerView * pickView;
    UIView * btmView;
    void (^selectedBlock)(NSString *);
}
@end

@implementation OneLinePickerView

-(instancetype)initWithDataArray:(NSArray *)dataArr
{
    if (self = [super init]) {
        
        dataArray = dataArr;
        
        float width = IPHONEWIDTH;
        float btnHeight = 40;
        float height = IPHONEHEIGHT;
        float pickHeight = 180;
        float btmViewHeight = pickHeight+btnHeight;
        
        self.frame = CGRectMake(0, 0, width, height);
        self.backgroundColor = COLOR_BLACK_TRANSPARENCY;
        
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
        [self addGestureRecognizer:ges];
        
        btmView = [[UIView alloc]initWithFrame:CGRectMake(0, height-btmViewHeight, width, btmViewHeight)];
        btmView.backgroundColor = [UIColor clearColor];
        [self addSubview:btmView];
        
        pickView = [[UIPickerView alloc]init];
        pickView.delegate = self;
        pickView.frame = CGRectMake(0,0, IPHONEWIDTH, pickHeight);
        
        pickView.backgroundColor = [UIColor whiteColor];
        [btmView addSubview:pickView];
        
        UIButton * confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        confirmBtn.frame = CGRectMake(0, btmViewHeight - btnHeight, width, btnHeight);
        [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [confirmBtn setTitleColor:COLOR_TEXT_BLACK forState:UIControlStateNormal];
        confirmBtn.backgroundColor = [UIColor whiteColor];
        [confirmBtn addTarget:self action:@selector(bgViewTapped:) forControlEvents:UIControlEventTouchUpInside];
        [btmView addSubview:confirmBtn];
        
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 0.5)];
        line.backgroundColor = FBEE_COLOR_LINE;
        [confirmBtn addSubview:line];
        
    }
    
    return self;
}

-(void)showInView:(UIView *)view selected:(void (^)(NSString * selectStr))selected
{
    
    
    selectedBlock = selected;
    
    [view addSubview:self];
    
    [AnimationHelper addPositionChangeForView:btmView fromOrigin:CGPointMake(0, IPHONEHEIGHT) toOrigin:CGPointMake(0, IPHONEHEIGHT-btmView.frame.size.height) duration:0.2 completedBlock:nil];
    [AnimationHelper addAlphaChangeForView:self fromFloat:0 toFloat:1 duration:0.2 completedBlock:nil];
    
}

-(void)bgViewTapped:(UIButton *)sender
{
    if (selectedBlock) {
        selectedBlock([dataArray objectAtIndex:[pickView selectedRowInComponent:0]]);
    }
    
    [self dismiss];
}
-(void)dismiss
{
    [AnimationHelper addPositionChangeForView:btmView fromOrigin:btmView.origin toOrigin:CGPointMake(0, self.frame.size.height) duration:0.2 completedBlock:nil];
    [AnimationHelper addAlphaChangeForView:self fromFloat:1 toFloat:0 duration:0.2 completedBlock:^{
        [self removeFromSuperview];
    }];
}

#pragma mark - UIPickerView

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return dataArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [dataArray objectAtIndex:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:18]];
        pickerLabel.textColor = [UIColor blackColor];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
    
}

-(void)setDefaultStr:(NSString *)defaultStr
{
    _defaultStr = defaultStr;
    
    NSInteger index = [dataArray indexOfObject:defaultStr];
    if (index<dataArray.count&&index>=0) {
        [pickView selectRow:index inComponent:0 animated:NO];
    }else{
        _defaultStr = nil;
    }
    
    
}
@end
